<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();
require("database/profile_handler.php");
require 'database/message_handler.php';
?>
<html>
    <head>
        <script type="text/javascript" src="script/ajax.js"></script>
        <link rel="stylesheet" href="style/style.css" />
    </head>
    <body>
        <?php
        $page = $_GET['page'];

        $m = new message_handler();
        $m->retr_message_col($_SESSION["Username"]);
        $pr = new profile_handler();

        $jml_page = ceil((count($m->result[0])) / 5);
        
//hitung dulu jumlah halaman berdasarkan jumlahnya
//per halaman ditampilkan 5 thread
//page
        //$pg = $page;
//echo $pg;
        echo "<div align='center'>";
        if ($_GET['page'] == 99) {
                $page = $jml_page;
                $pg = $page;
            }
            else{
                $pg = $jml_page;
            }
        echo "Page : ";
        for ($i = $jml_page; $i >= 1; $i--) {
        ?>
            <a id="headerLink" href="javascript:viewMessageList(<?php echo $pg; ?>)">
        <?php echo $i; ?>
            </a>
            <?php
            $pg--;
            echo " ";
        }
        echo "</div>";

        ?>
        <div id="message_content">
            <br/>
            <div id="page_title" align="center">
                <font color="green">Page <?php echo $page; ?></font>
            </div>
            <?php
            if (count($m->result) == 0) {
                echo "No messages";
            } else {

                $parse = $m->parse_group_members($m->result[3]);
                //pisahin disini
                //kasih paginasi
                //bikin div buat ajax
                for ($i = $jml_page; $i >= 1; $i--) {

                    $kali = $jml_page - $i;

                    $idxstart = count($m->result[0]) - (5 * $kali);
                    $idxend = $idxstart - 5 + 1;
                    if ($idxend < 0)
                        $idxend = 0;
                    $k = 0;
                    for ($j = $idxstart - 1; $j >= $idxend; $j--) {

                        $sub = str_replace(" ", "+", $m->result[0][$j]);
                        //subject//
                        //ini subject
                        $subject_str[$i][$k] = "Subject : " . "<a  id='headerLink' href='message_thread.php?msgid=" . $m->result[4][$j] . "&sub=" . $sub . "'>" . $m->result[0][$j] . "</a><br/>" . "<font color='green'>Between : </font>";
                        $arrname[$i][$k] = "";
                        for ($x = 0; $x < count($parse[$j]) - 1; $x++) {
                            //array of name lengkap
                            $pr->get_nama_lengkap($parse[$j][$x]);
                            if ($parse[$j][$x] == $_SESSION["Username"]) {
                                $arrname[$i][$k] .= "<a href='otherprofile.php?UserTujuan=" . $parse[$j][$x] . "' id='link_name'>" . You . "</a>, #";
                            } else {
                                $arrname[$i][$k] .= "<a href='otherprofile.php?UserTujuan=" . $parse[$j][$x] . "' id='link_name'>" . $pr->result[0] . "</a>, #";
                            }
                        }
                        $message_body[$i][$k] = "<br/>" .
                                "<br/><div id='message_body'>" .
                                $m->result[1][$j] . "<br/><br/>" .
                                "</div><br/><br/>";
                        $k++;
                    }
                }

                for ($i = 0; $i < count($subject_str[$jml_page]); $i++) {
                    //coba page terakhir dulu gan
                    if ($subject_str[$page][$i] != null) {
                        echo $subject_str[$page][$i];
                        //parsing arrname
                        $names = explode('#',$arrname[$page][$i]);
                        for ($s = 0; $s < count($names); $s++){
                            echo $names[$s];
                        }
                        echo $message_body[$page][$i];
                    }
                }
                //echo $subject_str[1][0];
            }
                ?>
        </div>
    </body>
</html>
